package cn.iocoder.yudao.module.blog.service.jiutagclassity;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.blog.controller.admin.jiutagclassity.vo.*;
import cn.iocoder.yudao.module.blog.dal.dataobject.jiutagclassity.BlogJiuTagClassityDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.blog.convert.jiutagclassity.BlogJiuTagClassityConvert;
import cn.iocoder.yudao.module.blog.dal.mysql.jiutagclassity.BlogJiuTagClassityMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.blog.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 案例标签分类 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class BlogJiuTagClassityServiceImpl implements BlogJiuTagClassityService {

    @Resource
    private BlogJiuTagClassityMapper jiuTagClassityMapper;

    @Override
    public Long createJiuTagClassity(BlogJiuTagClassityCreateReqVO createReqVO) {

        BlogJiuTagClassityDO jiuTagClassityDO = jiuTagClassityMapper.selectOne(BlogJiuTagClassityDO::getName, createReqVO.getName());
        if(jiuTagClassityDO != null){
            throw exception(JIU_TAG_CLASSITY_HAS_EXISTS);
        }

        // 插入
        BlogJiuTagClassityDO jiuTagClassity = BlogJiuTagClassityConvert.INSTANCE.convert(createReqVO);
        jiuTagClassityMapper.insert(jiuTagClassity);
        // 返回
        return jiuTagClassity.getId();
    }

    @Override
    public void updateJiuTagClassity(BlogJiuTagClassityUpdateReqVO updateReqVO) {
        // 校验存在
        validateJiuTagClassityExists(updateReqVO.getId());
        // 更新
        BlogJiuTagClassityDO updateObj = BlogJiuTagClassityConvert.INSTANCE.convert(updateReqVO);
        jiuTagClassityMapper.updateById(updateObj);
    }

    @Override
    public void deleteJiuTagClassity(Long id) {
        // 校验存在
        validateJiuTagClassityExists(id);
        // 删除
        jiuTagClassityMapper.deleteById(id);
    }

    private void validateJiuTagClassityExists(Long id) {
        if (jiuTagClassityMapper.selectById(id) == null) {
            throw exception(JIU_TAG_CLASSITY_NOT_EXISTS);
        }
    }

    @Override
    public BlogJiuTagClassityDO getJiuTagClassity(Long id) {
        return jiuTagClassityMapper.selectById(id);
    }

    @Override
    public List<BlogJiuTagClassityDO> getJiuTagClassityList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return jiuTagClassityMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<BlogJiuTagClassityDO> getJiuTagClassityPage(BlogJiuTagClassityPageReqVO pageReqVO) {
        return jiuTagClassityMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BlogJiuTagClassityDO> getJiuTagClassityList(BlogJiuTagClassityExportReqVO exportReqVO) {
        return jiuTagClassityMapper.selectList(exportReqVO);
    }

    @Override
    public List<BlogJiuTagClassityDO> getAll(){
        return jiuTagClassityMapper.selectList();
    }

}
